package leetcode;

import java.util.LinkedList;
import java.util.List;

class Solution_216 {
    List<List<Integer>> result = new LinkedList<>();
    List<Integer> res = new LinkedList<Integer>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        com(k,n,1,0);
        return result;
    }
    public void com(int k, int n,int start,int sum){
        if (res.size() >= k || sum > n){
            if (sum == n){
                result.add(new LinkedList<>(res));
            }
            return;
        }
        for (int i = start; i <= 9 - (k - res.size()) + 1; i++) {
            res.add(i);
            com(k,n,i + 1,sum + i);
            res.removeLast();
        }
    }
}